
"""
Tests for the common module.
"""

import threading
import time
import unittest
import mox
from gaweb import common


class SchedulerTest(unittest.TestCase):
    
    def test_schedule(self):
        # Tests scheduling a simple task and waiting for it to execute.
        d = {}

        def f():
            d['a'] = 3
            d['b'] = 4
            d['c'] = None

        common.scheduler(f, 1000)
        time.sleep(1.1)

        self.assertTrue('a' in d)
        self.assertTrue('b' in d)
        self.assertTrue('c' in d)
        self.assertEqual(d['a'], 3)
        self.assertEqual(d['b'], 4)
        self.assertEqual(d['c'], None)

    def test_neg_dt(self):
        # Tests that scheduler raises a ValueError for negative dt
        def f():
            pass
        self.assertRaises(ValueError, common.scheduler, f, -1)
        self.assertRaises(ValueError, common.scheduler, f, -5.0)
        self.assertRaises(ValueError, common.scheduler, f, -10)

    def test_zero_dt(self):
        # Tests that scheduler raises a ValueError for zero dt
        def f():
            pass
        self.assertRaises(ValueError, common.scheduler, f, 0)
        self.assertRaises(ValueError, common.scheduler, f, 0.0)


if __name__ == '__main__':
    unittest.main()
        
